/*=======================================================================================
	CategoryAggregation.do

		Aggregates categories following NIPA and CE rules as much as possilbe
		
	Author: Lorenz Kueng, April 2015
=========================================================================================*/


*-------------------------------------
* Spending aggregates
*-------------------------------------

foreach cat in 1 2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 19 20 21 30 50  {

 cap drop cat`cat'agg
 generate cat`cat'agg = cat`cat'
 
 foreach subcat in 01 02 03 04 05 06 07 08 09 10 11 {
 
	capture: replace cat`cat'agg = cat`cat'agg + cat`cat'`subcat'
 }
}



*-------------------------------------
* Approximating NIPA and CE aggregates
*-------------------------------------

#delim;

/* *********************************
              Expenditures
********************************** */

* nondurable;
cap drop temp;
generate temp = 0;
foreach cat in 
		611
		7
		701
		704
		706
		707
		708
		1401
		1702
	{;
	replace temp = temp + cat`cat';
};
rename temp nondur;
lab var nondur "nondurable expenditures";


* service;
cap drop temp;
generate temp = 0;
foreach cat in 
		1
		101
		102
		4
		403
		404
		406
		5
		501
		502
		503
		505
		506
		507
		508
		6
		602
		609
		9
		901
		902
		903
		10
		1001
		11
		1102
		1105
		1202
		1204
		1206
		13
		1301
		1302
		1303
		1304
		1306
		14
		1402
		1405
		1406
		15
		1501
		1502
		1503
		1504
		17
		1701
		1703
		1704
		1705
	{;
	replace temp = temp + cat`cat';
};
rename temp service;
lab var service "service expenditures";


* durable;
cap drop temp;
generate temp = 0;
foreach cat in 
		103
		104
		105
		2
		201
		202
		204
		206
		207
		606
		1003
		12
		1201
		1203
		1208
		1403
	{;
	replace temp = temp + cat`cat';
};
rename temp durable;
lab var durable "durable expenditures";


* mortgage and rent;
generate mortgage_rent = cat1207;
lab var  mortgage_rent "mortgage and rent";


* other expenditures;
cap drop temp;
generate temp = 0;
foreach cat in 
		8
		801
		802
		16
		1601
		1602
		1604
		1605
		1606
		1607
	{;
	replace temp = temp + cat`cat';
};
rename temp otherexp;
lab var otherexp "other expenditures (8: gifts & donations, 16: fees & charges";


* child support and allowances;
cap drop temp;
generate temp = 0;
foreach cat in
		603
		610
	{;
	replace temp = temp + cat`cat';
};
rename temp child_supp;
lab var child_supp "child support and allowance (603,610)";


* cash withdrawals;
cap drop temp;
generate temp = 0;
foreach cat in 
		2001
	{;
	replace temp = temp + cat`cat';
};
rename temp cash_withdrawals;
lab var cash_withdrawals "cash withdrawals (2001)";


* check deposits;
cap drop temp;
generate temp = 0;
foreach cat in 
		2002
	{;
	replace temp = temp + cat`cat';
};
rename temp check_payments;
lab var check_payments "check payments (2002)";


* ncategorized CC transactions;
cap drop temp;
generate temp = 0;
foreach cat in 
		20
	{;
	replace temp = temp + cat`cat';
};
rename temp uncategorized;
lab var uncategorized "uncategorized CC transactions (20)";


* user-defined categories;
cap drop temp;
generate temp = 0;
foreach cat in 
		9999
	{;
	capture: replace temp = temp + cat`cat';
};
rename temp user_defined;
lab var user_defined "user defined (9999)";

	
* unassigned transactions;
cap drop temp;
generate temp = 0;
foreach cat in 
		0
		40
		.
		999
		999999
	{;
	capture: replace temp = temp + cat`cat';
};
rename temp unassigned;
lab var unassigned "unassigned transactions (40,999,999999,0,missing)";


* Newly created category: Potentially miss-classified PFD transaction;
cap drop temp;
generate temp = 0;
foreach cat in
		3103
	{;
	replace temp = temp + cat`cat';
};
rename temp spending_txn_equal_PFD;
lab var spending_txn_equal_PFD "spending transactions that equal the PFD in the (603,610)";

#delim cr


* nondurables and service	
generate nondur_serv = nondur + service
lab var  nondur_serv "nondurables and services"
order    nondur_serv, before(nondur)




/* *********************************
    Income, Taxes, Investments
********************************** */

#delim;

* debt_pay;
cap drop temp;
generate temp = 0;
foreach cat in 
		1002
		1404
	{;
	replace temp = temp + cat`cat';
};
rename temp debt_pay;
lab var debt_pay "debt payments, based on accounts";


* income (mostly from labor);
cap drop temp;
generate temp = 0;
foreach cat in 
		30
		3001
		3003
		3004
		3005
		3006
		3007
		50
		5003
	{;
	replace temp = temp + cat`cat';
};
rename temp income;
lab var income "income, based on accounts";


* tax;
cap drop temp;
generate temp = 0;
foreach cat in 
		19
		1901
		1902
		1903
		1904
		1905
	{;
	replace temp = temp + cat`cat';
};
rename temp tax;
lab var tax "taxes";


* investment;
cap drop temp;
generate temp = 0;
foreach cat in 
		5004
		5005
	{;
	replace temp = temp + cat`cat';
};
rename  temp investment;
lab var investment "investments, based on accounts";


* transfers between accounts;
cap drop temp;
generate temp = 0;
foreach cat in 
		21
		2101
		2102
		5001
		5002
	{;
	replace temp = temp + cat`cat';
};
rename temp transfer;
lab var transfer "transer to other account";


* Newly created category: Income from PFD;
cap drop temp;
generate temp = 0;
foreach cat in
		3101
		3102
	{;
	cap n: replace temp = temp + cat`cat';
};
rename temp income_from_PFD;
lab var income_from_PFD "income from PFD payments (3101,3102)";

#delim cr
	
cap drop temp
